﻿let Glid = sessionStorage.getItem("id");

console.log(Glid);

if (Glid == "" || Glid == undefined) {
    location.href = "/Goods/GoodsTypeManager";
}

let Input = document.querySelector("#GoodName");

let Status = [{
    status: -1,
    name: '全部'
}, {
    status: 0,
    name: '已上架'
}, {
    status: 1,
    name: '未上架'
}];

let dto = {
    Glid: Glid,
    status: -1,
    key: ''
};




Peng_Select.CreateElement('Status_Select', Status, 'name', (res) => {
    dto.status = res.status;
    Input.value = "";
    dto.key = Input.value;
    GetGoods();
});


function GetGoods() {

    $.get("/Goods/GetGoods", { Glid: dto.Glid, status: dto.status, key: dto.key }).then((res) => {

        BindTable(res.data);
        console.log(res.data);
    });
}

function BindTable(obj) {

    layui.use('table', function () {
        var table = layui.table;

        //第一个实例
        table.render({
            elem: '#test'
            , loading: true,
            limit: 99999
            , data: obj //数据接口

            , cols: [[ //表头
                { field: 'name', title: '商品名称' },
                {
                    field: 'img', height: 100, templet: (d) => {
                        return `<img width='100px' src='${d.img}' />`;
                    }, title: '商品名称'
                },
                {
                    field: 'price', templet: (d) => {
                        return `￥ ${d.price} 元`;
                    }, title: '商品价格'
                },
                {
                    field: 'status', templet: (d) => {
                        if (d.status == 0) {
                            return `<span class='has'>已上架</span>`
                        } else {
                            return `<span class='wu'>已下架</span>`

                        }
                    }, title: '商品状态'
                },
                { field: 'num', title: '库存' },
                {
                    field: 'FlavorsTypeName', templet: (d) => {


                        if (d.FlavorsTypeName == null)
                            return `<span class='wu'><i class="fas fa-fire-alt"></i>无口味</span>`
                        else
                            return `<span class='has'><i class="fas fa-fire-alt"></i>${d.FlavorsTypeName}</span>`

                    }, title: '口味类型'
                },

                {
                    title: '操作', toolbar: '#barDemo'
                }
            ]]
        });




        table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event;


            //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            switch (layEvent) {
                case "XiaJia":
                 

                    $.post("/Goods/UpdateGoodStatus", { id: data.id, status: 1 }).then((res) => {
                        Input.value = "";
                        dto.key = Input.value;
                        Peng.Show("下架成功", { type: 'Success' });

                        GetGoods();
                    });
                    break;
                case "ShangJia":
                    $.post("/Goods/UpdateGoodStatus", { id: data.id, status: 0 }).then((res) => {
                        Input.value = "";
                        dto.key = Input.value;
                        Peng.Show("上架成功", { type: 'Success' });

                        GetGoods();
                    });
                    break;
                case "detail":
                    sessionStorage.setItem('id', data.id);
                    location.href = "/Goods/GoodDetailsHtml";
                    break;
            }
        });


    });
}

var Query = function () {

    dto.key = Input.value;
    GetGoods();
    Input.value = "";
}



